package i_thread;

public class ThreadWaitTest {

	public static void main(String[] args) {
		Message obj = new Message();
		ReadThread1 th1 = new ReadThread1(obj);
		WriteThread1 th2 = new WriteThread1(obj);
		
		th2.start();		
		th1.start();
	}
}

class Message {
	boolean status = false;
	StringBuilder msg = new StringBuilder();

	
	public void setMsg(String s){
		msg.append(s);
	}
	
	public String getMsg(){
		return msg.toString();
	}
}

class ReadThread1 extends Thread {
	Message obj;
	
	public ReadThread1(Message obj){
		this.obj = obj;
	}
	
	
	@Override
	public void run(){
		synchronized (obj) {
			try{
				Thread.sleep(100);
			}catch(Exception ex){}
			
			for(char a = 'A' ; a<= 'Z' ; a++){
				obj.setMsg(String.valueOf(a));
			}
			System.out.println("Message에 저장완료");
			obj.status = true;
			obj.notify();
		}
	}
}

class WriteThread1 extends Thread {
	Message obj;
	
	public WriteThread1(Message obj){
		this.obj = obj;
	}
	
	@Override
	public void run(){
		synchronized (obj) {
			if(!obj.status){
				try{
					//obj.wait();
				}catch(Exception ex){
					
				}
			}
			
			System.out.println("저장된 데이터를 출력");
			System.out.println(obj.getMsg());
		}
	}
}
